package com.lagou.v4;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Kirito
 * @description:
 * @date 2020/04/24
 */
public class Bootstrap {

    private Object serverDaemon = null;

    private static Bootstrap daemon = null;

    /**
     * Minicat v3.0
     * 需求：可以请求动态资源（Servlet）
     */
    public void start() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String methodName = "start";
        Method method = serverDaemon.getClass().getMethod(methodName, (Class[]) null);
        method.invoke(serverDaemon, (Object[]) null);
    }


    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.init();
            daemon = bootstrap;
            daemon.load(args);
            daemon.start();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void load(String[] arguments) throws Exception {
        String methodName = "load";
        Object param[];
        Class<?> paramTypes[];
        if (arguments == null || arguments.length == 0) {
            paramTypes = null;
            param = null;
        } else {
            paramTypes = new Class[1];
            paramTypes[0] = arguments.getClass();
            param = new Object[1];
            param[0] = arguments;
        }
        Method method =
                serverDaemon.getClass().getMethod(methodName, paramTypes);

        method.invoke(serverDaemon, param);
    }

    private void init() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Object startupInstance = Class.forName("com.lagou.v4.container.Server").getConstructor().newInstance();
        serverDaemon = startupInstance;
    }
}
